package cn.itcast.ssm.controller;

import cn.itcast.ssm.po.UserInf;
import cn.itcast.ssm.service.UserService;
import cn.itcast.ssm.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @author tyh
 * @Package cn.itcast.ssm.controller
 * @Description: userController
 * @date 2018/5/9 15:12
 */
@Controller
@RequestMapping("/html")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param session session
     * @param map2 手机号
     * @return 是否发送成功
     */
    @ResponseBody
    @RequestMapping(value = "/sendCode",method = RequestMethod.POST)
    public Map<String ,Object> sendCode(HttpSession session, @RequestBody Map<String,Object> map2){
        Map<String ,Object> map=new HashMap<>();
        if(map2.get("tel")!=null&&!map2.get("tel").equals("")){
            String tel=String.valueOf(map2.get("tel"));
            System.out.println("手机号为:"+map2.get("tel"));
            String[] str={"1","2","3","4","5","6","7","8","9","0"};
            String s="";
            for (int j = 0; j <6 ; j++) {
                s+=str[(int)(Math.random()*(str.length))];
            }
            System.out.println(s);
            session.setAttribute("code",s);
            map.put("mark","success");
        }else{
            map.put("mark","error");
        }
        return map;
    }

    /**
     * 用户注册
     * @param user 用户信息
     * @param code 验证码
     * @param session session
     * @param response response
     * @return 界面
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(UserInf user, String code, HttpSession session, HttpServletResponse response){
        if(session.getAttribute("code")!=null){
            if(code.equals(session.getAttribute("code"))){
                if(user.getUserid()!=null&&user.getPassword()!=null&&user.getTel()!=null){
                    user.setPassword(EncryptUtil.MD5Encrpt(user.getPassword()));
                    if(userService.register(user)){
                        return "redirect:/index.html";
                    }
                }else{
                    Cookie cookie=new Cookie("msg","信息不完整！");
                    cookie.setMaxAge(3600);
                    response.addCookie(cookie);
                }
            }else{
                Cookie cookie=new Cookie("msg","验证码错误！");
                cookie.setMaxAge(3600);
                response.addCookie(cookie);
            }
        }else{
            Cookie cookie=new Cookie("msg","请验证手机！");
            cookie.setMaxAge(3600);
            response.addCookie(cookie);
        }
        return "redirect:/html/newuser.html";
    }

}
